package org.tity.support;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.tity.commons.environment.PathTool;
import org.tity.commons.io.EasyFile;
import org.tity.commons.lang.DataParser;
import org.tity.commons.lang.DateUtil;
import org.tity.support.pojo.Person;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
 
 

public class PojoTest {
	
	
	
	
	 
//	private static class MyASMDeserializerFactory extends ASMDeserializerFactory
//	{
//
//		public MyASMDeserializerFactory(ClassLoader parentClassLoader) {
//			super(parentClassLoader);
//		}
//		
//		public ObjectDeserializer createJavaBeanDeserializer(ParserConfig config, JavaBeanInfo beanInfo) throws Exception {
//	        Class<?> clazz = beanInfo.clazz;
//	        if (clazz.isPrimitive()) {
//	            throw new IllegalArgumentException("not support type :" + clazz.getName());
//	        }
//
//	        String className = "FastjsonASMDeserializer_" + seed.incrementAndGet() + "_" + clazz.getSimpleName();
//	        String classNameType;
//	        String classNameFull;
//
//	        Package pkg = ASMDeserializerFactory.class.getPackage();
//	        if (pkg != null) {
//	            String packageName = pkg.getName();
//	            classNameType = packageName.replace('.', '/') + "/" + className;
//	            classNameFull = packageName + "." + className;
//	        } else {
//	            classNameType = className;
//	            classNameFull = className;
//	        }
//
//	        ClassWriter cw = new ClassWriter();
//	        cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, classNameType, type(JavaBeanDeserializer.class), null);
//
//	        _init(cw, new Context(classNameType, config, beanInfo, 3));
//	        _createInstance(cw, new Context(classNameType, config, beanInfo, 3));
//	        _deserialze(cw, new Context(classNameType, config, beanInfo, 5));
//
//	        _deserialzeArrayMapping(cw, new Context(classNameType, config, beanInfo, 4));
//	        byte[] code = cw.toByteArray();
//
//	        Class<?> deserClass = classLoader.defineClassPublic(classNameFull, code, 0, code.length);
//	        Constructor<?> constructor = deserClass.getConstructor(ParserConfig.class, JavaBeanInfo.class);
//	        Object instance = constructor.newInstance(config, beanInfo);
//
//	        return (ObjectDeserializer) instance;
//	    }
//		
//	}
	
//	private static class BeanProcessor implements ExtraTypeProvider,ExtraProcessor,FieldTypeResolver
//	{
//
//		@Override
//		public Type resolve(Object object, String fieldName) {
//			// TODO Auto-generated method stub
//			return null;
//		}
//
//		@Override
//		public void processExtra(Object object, String key, Object value) {
//			// TODO Auto-generated method stub
//			System.out.println();
//		}
//
//		@Override
//		public Type getExtraType(Object object, String key) {
//			// TODO Auto-generated method stub
//			return null;
//		}
//		
//	}
	

	@Test
	public void pojo_test() throws Exception
	{
		EasyFile file=new EasyFile(PathTool.getPath(PojoTest.class));
		String str=file.resolve("relationship.json").getStringContent();
		//JSONObject relationship=JSONObject.parseObject(str);
		
//		Person leefj=JSONObject.parseObject(relationship.toJSONString(), Person.class);
	 
		Person leefj = null;
		try {
			leefj = JSON.parseObject(str, Person.class);
		} catch (Exception e) {
			 
//			e.printStackTrace();
		}
		
//		,new BeanProcessor(), 1,Feature.AllowComment,Feature.AllowArbitraryCommas,
//		Feature.AllowISO8601DateFormat,Feature.AllowSingleQuotes,Feature.AllowUnQuotedFieldNames
		
		assertTrue(DateUtil.isSameDay(leefj.getBirthday(), DataParser.parseDate("19811122")));
		assertTrue(leefj.getName()!=null);
		
		//
		Person f1=leefj.getFather();
		assertTrue(f1!=null);
		assertTrue(f1.getName().equals("f1"));
		assertTrue(DateUtil.isSameDay(f1.getBirthday(), DataParser.parseDate("19811122")));
		
		//
		Person f2=f1.getFather();
		assertTrue(f2!=null);
		assertTrue(f2.getName().equals("f2"));
		assertTrue(DateUtil.isSameDay(f2.getBirthday(), DataParser.parseDate("19811122")));
		
		//
		assertTrue(leefj.getChildren()!=null);
		assertTrue(leefj.getChildren().size()==2);
		
		Person c1=leefj.getChildren().get(0);
		assertTrue(c1!=null);
		assertTrue(c1.getName().equals("c1"));
		assertTrue(DateUtil.isSameDay(c1.getBirthday(), DataParser.parseDate("19811122")));
		
		
		//
		assertTrue(leefj.getFriends()!=null);
		assertTrue(leefj.getFriends().length==2);
		
		Person fr1=leefj.getFriends()[0];
		assertTrue(fr1!=null);
		assertTrue(fr1.getName().equals("fr1"));
		assertTrue(DateUtil.isSameDay(fr1.getBirthday(), DataParser.parseDate("19811122")));
		
		//
		assertTrue(leefj.getStudents()!=null);
		assertTrue(leefj.getStudents().size()==2);
		
		Person st2=leefj.getStudents().iterator().next();
		assertTrue(st2!=null);
		assertTrue(st2.getName().equals("st2") || st2.getName().equals("st1"));
		assertTrue(DateUtil.isSameDay(st2.getBirthday(), DataParser.parseDate("19811122")));
		
		System.out.println();
		
	}
	
	public static void main(String[] args) throws Exception {

        ExtraProcessor processor = new ExtraProcessor() {
            public void processExtra(Object object, String key, Object value) {

                System.out.println("---------------object = " + object);
                System.out.println("---------------key = " + key);
                System.out.println("---------------value = " + value);
                System.out.println();

                Person vo = (Person) object;
               // vo.setId(789);// 修改一下id值
               // vo.getAttributes().put(key, value);
            }
        };
        
        EasyFile file=new EasyFile(PathTool.getPath(PojoTest.class));
		String str=file.resolve("relationship.json").getStringContent();
        
        // 这里name和phone是多余的，在VO里没有
//        Person vo = JSON.parseObject("{\"id\":123,\"name\":\"abc\",\"phone\":\"18603396954\"}", Person.class, processor);
        Person leefj = JSON.parseObject(str, Person.class);
//        Person vo = JSON.parseObject(str, Person.class, processor);

        //System.out.println("vo.getId() = " + vo.getId());
        //System.out.println("vo.getAttributes().get(\"name\") = " + vo.getAttributes().get("name"));
        //System.out.println("vo.getAttributes().get(\"phone\") = " + vo.getAttributes().get("phone"));
    } 
	
}
